package com.smartcommunity.elasticsearch;

import com.smartcommunity.es.common.EsIndex;
import com.smartcommunity.es.common.EsRestUtils;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author nieming
 * @descrition
 * @date 2020/6/5
 */
@Component
@Slf4j
public class StatisticEsApi {
    @Autowired
    private EsRestUtils eSRestUtils;

    public Long getAllAccessCount() {
        try {
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.ACCESSRECORD_ALIAS}, QueryBuilders.matchAllQuery());
        } catch (IOException e) {
            log.error("查询门禁数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
    public Long getAccessCountByTime(Long timeStart,Long timeEnd) {
        try {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").gte(timeStart));
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").lt(timeEnd));
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.ACCESSRECORD_ALIAS}, boolQueryBuilder);
        } catch (IOException e) {
            log.error("查询门禁数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
    public Long getAllFaceCount() {
        try {
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.FACE_ALIAS}, QueryBuilders.matchAllQuery());
        } catch (IOException e) {
            log.error("查询人脸数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
    public Long getAllFaceCountByTime(Long timeStart,Long timeEnd) {
        try {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").gte(timeStart));
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").lt(timeEnd));
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.FACE_ALIAS}, boolQueryBuilder);
        } catch (IOException e) {
            log.error("查询人脸数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
    public Long getAllVehicleCount() {
        try {
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.VEHICLE_ALIAS}, QueryBuilders.matchAllQuery());
        } catch (IOException e) {
            log.error("查询车辆数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
    public Long getAllVehicleCountByTime(Long timeStart,Long timeEnd) {
        try {
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").gte(timeStart));
            boolQueryBuilder.must(QueryBuilders.rangeQuery("mrowTime").lt(timeEnd));
            return eSRestUtils.getTotalRecords(new String[]{EsIndex.VEHICLE_ALIAS}, boolQueryBuilder);
        } catch (IOException e) {
            log.error("查询车辆数据接入数据总量失败,e:", e);
            return 0L;
        }
    }
}
